/**
 * 
 */
package com.yunyike.controller.user;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yunyike.constants.RequestConstant;
import com.yunyike.constants.ResponseConstant;
import com.yunyike.controller.BaseController;
import com.yunyike.model.Recepient;
import com.yunyike.model.User;
import com.yunyike.service.ICouponService;
import com.yunyike.service.IOrderService;
import com.yunyike.service.IRecepientService;
import com.yunyike.service.IUserService;
import com.yunyike.utils.JsonUtils;
import com.yunyike.wx.model.UserInfo;
import com.yunyike.wx.service.IWxUserService;

/**
 * @author zhengqiang.shi
 * 2015年11月21日 下午6:15:28
 */
@Controller
@RequestMapping(value=RequestConstant.USER)
public class UserController extends BaseController {

	private Logger log = LoggerFactory.getLogger(UserController.class);
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private ICouponService couponService;
	
	@Autowired
	private IOrderService orderService;
	
	@Autowired
	private IRecepientService recepientService;
	
	@Autowired
	private IWxUserService wxUserService;
	
	/**
	 * 隐形注册+登录
	 * @param shopId
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value=RequestConstant.USER_ADD_INVISIBLE+"/{openId}")
	public String addUserInvisible(@PathVariable String openId,HttpServletRequest request,
			HttpServletResponse response,Model model){
		
		log.info("controller add user...");
		
		User user = new User();
		user.setUserOpenId(openId);
		
		// 隐形注册
		int result = userService.addUserInvisible(user);
		
		if(result != 1){
			log.info("controller add user end. but failed.");
			return ResponseConstant.FAILED;
		}
		
		// 登录
		log.info("controller user login...");
		userService.login(openId, request, response);
		
		log.info("controller add user end.");
		return ResponseConstant.SUCCESS;
	}
	
	/**
	 * 个人中心主页
	 * @param openId
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value=RequestConstant.USER_INDEX)
	public String index(HttpServletRequest request,
			HttpServletResponse response,Model model){
		
		log.info("controller user to index...");
		
		User user = getCurrentUser();
		user = userService.findUserById(user.getUserId());
		
		List<Recepient> recepientList = recepientService.findRecepientByUser(user.getUserId());
		
		if(recepientList != null && !recepientList.isEmpty()){
			model.addAttribute("recepient", recepientList.get(0));
		}
		
		UserInfo userInfo = wxUserService.getUserInfo(user.getUserOpenId());
		
		model.addAttribute("user", user);
		model.addAttribute("userInfo", userInfo);
		
		return ResponseConstant.USER_MAIN;
	}
	
	/**
	 * 个人中心主页查询 
	 * 现金券、物流券、待收货订单、已完成订单数量
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value=RequestConstant.USER_COUPON_ORDER_QTY)
	public String findQty(){
		
		User user = getCurrentUser();
		
		Map<String, Integer> findAvailiableCouponQty = couponService.findAvailiableCouponQty(user);
		Map<String, Integer> findOrderCountByCondition = orderService.findOrderCountByCondition(user);
		
		findAvailiableCouponQty.putAll(findOrderCountByCondition);
		
		return JsonUtils.ObjectAsString(findAvailiableCouponQty);
	}
	
	@ResponseBody
	@RequestMapping(value=RequestConstant.USER_ADD_TEL+"/{telNumber}")
	public String addUserTel(@PathVariable String telNumber,HttpServletRequest request,
			HttpServletResponse response,Model model){
		
		log.info("controller addUserTel...");
		User user = getCurrentUser();
		
		user.setUserTel(telNumber);
		try{
			int result = userService.update(user);
		}catch(Exception e){
			log.info("controller addUserTel..error.");
			e.printStackTrace();
			return "error";
		}
		log.info("controller addUserTelend.");
		return "success";
	}
	@RequestMapping(value=RequestConstant.USER_TEL)
	public String userTel(HttpServletRequest request,
			HttpServletResponse response,Model model){
		
		log.info("controller userTel..");
		
		User user = getCurrentUser();
		user = userService.findUserById(user.getUserId());
		
		
		model.addAttribute("user", user);
		
		return ResponseConstant.USER_TEL;
	}
	
}
